package com.user.controller;


import com.blog.security.service.TokenService;
import com.blog.security.utils.SecurityUtils;
import com.comon.core.domain.result.R;
import com.comon.core.utils.PasswordUtil;
import com.comon.core.utils.StringUtils;
import com.user.domain.User.DTO.UserDTO;
import com.user.domain.User.DTO.UserRegDTO;
import com.user.domain.concern.DTO.UserFavorDTO;
import com.user.service.concern.UserFavorService;
import com.user.service.user.impl.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.javassist.compiler.ast.Variable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-08-24
 * Time:15:21
 * Text：用户登录注册
 */
@RestController
@Tag(name="用户登录注册接口")
public class UserController {
    @Autowired
    private UserService service;
    @Autowired
    private UserFavorService userFavorService;
    @Autowired
    private PasswordUtil passwordUtil;
    @Autowired
    private TokenService tokenService;

    @Operation(summary = "普通用户登录")
    @ApiResponse(responseCode = "3004",description = "验证码错误")
    @ApiResponse(responseCode = "1000",description = "登录成功")
    @ApiResponse(responseCode = "3103",description = "账户或者密码错误")
    @ApiResponse(responseCode = "3102",description = "账户不存在")
    @PostMapping("/login")
    public R<?> login(@RequestBody UserDTO userDTO, HttpServletRequest request){
        HttpSession session = request.getSession(false);
        System.out.println(session.getAttribute("verifyCode"));
        String verifyCode = session.getAttribute("verifyCode").toString();
        boolean t = passwordUtil.decrypt(userDTO.getCaptcha(), verifyCode);
        if(t){
            return service.login(userDTO,request);
        }
            return R.fail(3004,"验证码错误");
    }


    @Operation(summary = "普通用户注册")
    @ApiResponse(responseCode = "3000",description = "操作失败")
    @ApiResponse(responseCode = "3101",description = "用户已存在")
    @ApiResponse(responseCode = "3002",description = "密码太短")
    @ApiResponse(responseCode = "1000",description = "注册成功")
    @PostMapping("reg")
    public R<?> reg(@RequestBody UserRegDTO userRegDTO){
        return service.reg(userRegDTO);
    }


    @DeleteMapping("/logout")
    public R<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            tokenService.delLoginUser(token);
        }
        return R.ok();
    }
    @Operation(summary = "关注殿堂")
    @PostMapping("/concern")
    public R<?> concern(@RequestBody UserFavorDTO userFavorDTO, HttpServletRequest request){
        Long Uid = Long.parseLong(request.getSession().getAttribute("UserId").toString());
        return userFavorService.concern(Uid,userFavorDTO);
    }


    @Operation(summary = "普通用户退出登录")
    @ApiResponse(responseCode = "1000",description = "退出成功")
    @DeleteMapping("/LayoutInfo")
    public R<?> LayoutInfo(String token) {
        return service.layoutinfo(token);
    }

    @Operation(summary = "根据ID获取用户名")
    @GetMapping("/getName/{id}")
    public String getName(@PathVariable Long id){
        return service.getName(id);
    }

    @Operation(summary = "根据电话号码获取用户名")
    @GetMapping("/Info/{uphone}")
    public R<?> getInfo(@PathVariable int uphone){
        return service.getUPhone(uphone);
    }

    @Operation(summary = "根据Id获取头像")
    @GetMapping("/getPhotoInfo/{id}")
    public String getPhotoInfo(@PathVariable Long id){
        return service.getPhotoInfo(id);
    }
}
